package data;
import domain.Usuario;
import java.sql.*;
import java.util.List;
import java.util.LinkedList;

public class JDBCUsuarioDAO implements IUsuarioDAO{
	
	public List selectAllUsuarios (){
		
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		ResultSet result = null;
		List listaUsuarios = new LinkedList();
		String sql = "SELECT * FROM Usuario ";
		try {
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();
			while (result.next()) {
				Usuario user = rellenarUsuario(result);
				listaUsuarios.add(user);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return listaUsuarios;
	}

	public Usuario select(String nick){
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		ResultSet result = null;
		Usuario user=null;
		String sql = "SELECT * FROM Usuario WHERE (NICK = ?) ";
		try {	
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, nick);
			result = stmt.executeQuery();
			while(result.next()){
				user = rellenarUsuario(result);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return user;
	}
	
	public void delete(String nick) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		String sql = "DELETE FROM Usuario WHERE (NICK = ?) ";
		try {	
			stmt = conn.prepareStatement(sql);
            stmt.setString(1, nick);
            stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		
	}

	public void insert(Usuario usuario) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
		String sql = "INSERT INTO Usuario (OID,NICK,PASSWORD,NOMBRE,PROVINCIA,MUNICIPIO,CORREO,TELEFONO,SKINOID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ";
		try {	
			stmt = conn.prepareStatement(sql);
            stmt.setString(1, usuario.getUsuarioOID());
            stmt.setString(2, usuario.getNick());
            stmt.setString(3, usuario.getPassword());
            stmt.setString(4, usuario.getNombre());
            stmt.setString(5, usuario.getProvincia());
            stmt.setString(6, usuario.getMunicipio());
            stmt.setString(7, usuario.getCorreo());
            stmt.setString(8, usuario.getTelefono());
            stmt.setString(9, usuario.getSkin());
            stmt.executeUpdate();
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
	}
	
	private Usuario rellenarUsuario(ResultSet result) throws SQLException {
		Usuario user = new Usuario();
		user.setUsuarioOID(result.getString("OID"));
		user.setNombre(result.getString("NOMBRE"));
		user.setNick(result.getString("NICK"));
		user.setPassword(result.getString("PASSWORD"));
		user.setProvincia(result.getString("PROVINCIA"));
		user.setMunicipio(result.getString("MUNICIPIO"));
		user.setCorreo(result.getString("CORREO"));
		user.setTelefono(result.getString("TELEFONO"));
		user.setSkin(result.getString("SKIN"));
		return user;
	}

}



